/*
 * MenuItemDelegate.cpp  0.4.0 / Dec 04, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "MenuItemDelegate.h"

#include "QApplicationDelegate.h"
#include "QIconAdapter.h"

// MenuItemDelegate :: Protected Constructor. ----------------------------------

/*!
 * \brief Constructs new MenuItemDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the MenuItem java object
 */
MenuItemDelegate::MenuItemDelegate(JNIEnv *env, jobject obj)
  : ActionComponentDelegate(env, obj), m_shortcut(0)
{
  // empty constructor body
}

// MenuItemDelegate :: Native Calls. -------------------------------------------

/*!
 * \brief Sets text
 *
 * \param env the JNIEnv pointer
 * \param text the text
 */
void MenuItemDelegate::setText(JNIEnv *env, jstring text)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setText, SWING_ENV_ARG, JSTRING_ARG(text))
  if (isQWidget()) {
    label()->setText(QStringAdapter(env, text));
  } else {
    action()->setText(QStringAdapter(env, text));
  }
}

/*!
 * \brief Sets short cut
 *
 * \param env the JNIEnv pointer
 * \param shortcut the shortcut
 */
void MenuItemDelegate::setShortcut(JNIEnv *env, jstring shortcut)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setShortcut, SWING_ENV_ARG, JSTRING_ARG(shortcut))
  QStringAdapter shortcutString(env, shortcut);
  if (isQWidget()) {
    if (!m_shortcut) {
      m_shortcut = new QShortcut(widget());
    }
    if (shortcutString.isEmpty()) {
      m_shortcut->setKey(QKeySequence::UnknownKey);
    } else {
      m_shortcut->setKey(QKeySequence(shortcutString));
    }
  } else {
    if (shortcutString.isEmpty()) {
      action()->setShortcut(QKeySequence::UnknownKey);
    } else {
      action()->setShortcut(QKeySequence(shortcutString));
    }
  }
}

/*!
 * \brief Sets icon
 *
 * \param env the JNIEnv pointer
 * \param iconSet the icon
 */
void MenuItemDelegate::setIcon(JNIEnv *env, jobject iconSet)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setIcon, SWING_ENV_ARG, JOBJECT_ARG(iconSet))
  if (isQWidget()) {
    if (iconSet) {
      label()->setIcon(QIconAdapter(env, iconSet));
    } else {
      label()->setIcon(QIcon());
    }
  } else {
    if (iconSet) {
      action()->setIcon(QIconAdapter(env, iconSet));
    } else {
      action()->setIcon(QIcon());
    }
  }
}

// MenuItemDelegate :: Static Native Calls. ------------------------------------

/*!
 * \brief Initializes MenuItemDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the MenuItem java class
 *
 * \exception FatalError if failed
 */
void MenuItemDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT (!sm_actionTriggeredMID);
  if (!INIT_UMID(actionTriggered, cls, "(I)V")) {
    FATAL_LOGGED_ERROR("MenuItemDelegate initialization failed");
  }
}

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong MenuItemDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new MenuItemDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}

// MenuItemDelegate :: Native Callbacks. ---------------------------------------

/*!
 * \brief Calls \c actionTriggered method of the MenuItemDelegate object.
 *
 * \param modifiers the modifiers
 */
void MenuItemDelegate::actionTriggered(jint modifiers)
{
  jniObject()->callVoidMethod(sm_actionTriggeredMID, modifiers);
}

// MenuItemDelegate :: Protected Member Function. ------------------------------

/*!
 * \brief Triggered lot.
 */
void MenuItemDelegate::triggeredSlot()
{
  actionTriggered(QApplicationDelegate::keyboardModifiers());
}

// MenuItemDelegate :: Overridden Member Functions. ----------------------------

/*!
 * \brief Creates QWidget adaptee.
 *
 * \return QWidget adaptee
 */
QWidget *MenuItemDelegate::createQWidget()
{
  return new QLabelAdapter();
}

/*!
 * \brief Creates QAction adaptee.
 *
 * \return QAction adaptee
 */
QAction *MenuItemDelegate::createQAction()
{
  QAction *action = new QAction(this);
  connectDirect(action, SIGNAL(triggered()), SLOT(triggeredSlot()));
  return action;
}

/*!
 * \inheritDoc
 */
void MenuItemDelegate::deleteQWidget(QWidget *widget)
{
  if (m_shortcut) {
    delete m_shortcut;
    m_shortcut = 0;
  }
  ActionComponentDelegate::deleteQWidget(widget);
}

/*!
 * \inheritDoc
 */
void MenuItemDelegate::deleteQAction(QAction *action)
{
  disconnect(action, SIGNAL(triggered()), SLOT(triggeredSlot()));
  ActionComponentDelegate::deleteQAction(action);
}

/*!
 * \inheritDoc
 */
QWidget *MenuItemDelegate::convertToQWidget(QAction *action)
{
  QLabelAdapter *label = static_cast<QLabelAdapter *>(
                             ActionComponentDelegate::convertToQWidget(action));
  label->setText(action->text());
  if (m_shortcut) {
    delete m_shortcut;
  }
  m_shortcut = new QShortcut(label);
  m_shortcut->setKey(action->shortcut());
  label->setIcon(action->icon());
  return label;
}

/*!
 * \inheritDoc
 */
QAction *MenuItemDelegate::convertToQAction(QWidget *widget)
{
  QLabelAdapter *label = static_cast<QLabelAdapter *>(widget);
  QAction *action = ActionComponentDelegate::convertToQAction(widget);
  action->setText(label->text());
  if (m_shortcut) {
    action->setShortcut(m_shortcut->key());
  }
  action->setIcon(label->icon());
  return action;
}

// MenuItemDelegate :: Private Static Data Member. -----------------------------

jmethodID MenuItemDelegate::sm_actionTriggeredMID = 0;
